package View;

import java.awt.Canvas;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.swing.*;
import main.GamesLocator;
import main.LotusEngine;
import main.ResourceFile;

import Game.AbstractGame;


@SuppressWarnings("serial")
public class LotusViewer extends JFrame
{

    private LotusEngine myEngine;
    private Canvas myCanvas;
	protected static final JFileChooser myChooser = 
		new JFileChooser(System.getProperty("user.dir"));

    public LotusViewer (LotusEngine engine)
    {
        myEngine = engine;
        
        this.setSize(600, 600);
        this.setVisible(true);
        this.setResizable(false);
        this.setTitle("Lotus Arcade");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
       	makeMenus();

        //select game on startup
        swapView(new GameSelectionView(this));

    }
    
    private void makeMenus ()
    {
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(makeFileMenu());

        this.setJMenuBar(menuBar);
    }
    
    private JMenu makeFileMenu()
    {
    	JMenu fileMenu = new JMenu(ResourceFile.getString("file"));

    	fileMenu.add(new AbstractAction(ResourceFile.getString("gameselect"))
    	{
			public void actionPerformed(ActionEvent e) 
			{
			    //TODO
				//showGameSelectionView();				
			}	
    	});
    	
    	fileMenu.add(new AbstractAction(ResourceFile.getString("changegamesource"))
    	{
    		public void actionPerformed(ActionEvent e)
    		{    			
    			String location = browserGetFile();
    			GamesLocator.saveLocation(location);
    		}
    	});
        
        fileMenu.addSeparator();

        fileMenu.add(new AbstractAction(ResourceFile.getString("exit"))
        {
            public void actionPerformed (ActionEvent e)
            {
                System.exit(0);
            }
        });
        
        return fileMenu;
    }
    
	/**
	 * Opens pop-up browser where a directory or file can be selected.  
	 * The directory path is then sent to the Controller.
	 * @return TODO
	 */
	private String browserGetFile() {
		int retval = myChooser.showOpenDialog(null);
		if (retval == JFileChooser.APPROVE_OPTION)
		{
			File file = myChooser.getSelectedFile();
			if(file.exists())
				return file.getPath();
			else
				showError(ResourceFile.getString("nofile") + file.getName());
		}
		return null;
	}
	
	/**
	 * Prints an error message to the user.
	 * @param s
	 */
	private void showError (String s)
	{
		JOptionPane.showMessageDialog(this, s, ResourceFile.getString("error"),
				JOptionPane.ERROR_MESSAGE);
	}

    protected void swapView (JComponent c)
    {
        getContentPane().removeAll();
        add(c);
        validate();
    }
    
    public void loadEngine(LotusEngine a)
    {
        myEngine = a;
    }
    
    public void loadGame(AbstractGame game)
    {
    	myEngine.loadGame(game);
    }
    
    public void newCanvas()
    {
        this.removeAll();
        myCanvas = new Canvas();
        this.add(myCanvas);
    }
    
    public void paintSprite(Image img, Point p)
    {
        Graphics g = myCanvas.getGraphics();
        g.drawImage(img, p.x, p.y, null);
        
    }
}
